const { request } = require('../../../../services/request')
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    userInfo: Object,
    // refresh: Object,
    // fromMenu: Boolean,
  },

  /**
   * 组件的初始数据
   */
  data: {
    visible: false,
    // avatar: JSON.parse(wx.getStorageSync('userInfo'))
  },

  /**
   * 组件的方法列表
   */
  methods: {
    handleEdit(e) {
      wx.navigateTo({
        url: `/pages/edit-user/index?type=edit&id=${e.target.dataset.id}`,
      })
    },
    handelSelect(e){
      console.log('handel select', this.data.userInfo)
              let params = {
          answeUserId: this.data.userInfo.answeUserId,
          userId: this.data.userInfo.userId,
        }
      this.triggerEvent('selectUser', params)
    },
    handleDel() {
      this.setData({
        visible: true,
      })
    },
    refresh() {
      this.triggerEvent('refresh')
    },
    handleSetDefault(e) {
      if (!this.data.fromMenu) {
        if (this.data.userInfo.isDefault === 0) {
          request('POST', `/applet/answer/user/setDefault`, {
            answeUserId: this.data.userInfo.answeUserId,
          })
            .then(this.refresh())
            .catch()
        }
      } else {
        let params = {
          answeUserId: this.data.userInfo.answeUserId,
          userId: this.data.userInfo.userId,
        }
        this.triggerEvent('selectUser', params)
      }
    },
    handleCancel() {
      this.setData({
        visible: false,
      })
    },
    handleCancel() {
      this.setData({
        visible: false,
      })
    },
    handleConfirm() {
      request(
        'POST',
        `/applet/answer/user/delete/${this.data.userInfo.answeUserId}`
      )
        .then((res) => {
          this.setData({
            visible: false,
          })
          this.refresh()
        })
        .catch()
    },
  },
})
